package bingo.security.datasecurity.impl;

import bingo.enviroment.core.EnviromentHolder;
import bingo.security.datasecurity.core.ISecurityRuleParser;

/**
 * 2008-4-23 下午07:18:37
 * Author:yangdongyu(yangdy@bingosoft.net)
 * ReadMe:默认的环境变量解析器,实现规则环境变量的解析
 * ChangeLog:
 */
public class DefaultRuleParser  implements ISecurityRuleParser{
	
	
	
	/**
	 * 实现规则解析
	 */
	public String parseRule(String action,String rule) {
		return parseEnviromentVariable(rule, '{', '}');

	}
    /**
     * 实现环境变量的解析
     * @param content 待解析的内容字符串
     * @param variablePrefix 环境变量前缀
     * @param variableSurfix 环境变量的后缀
     * @return 解析之后的内容
     */
	protected String parseEnviromentVariable(String content,char variablePrefix,char variableSurfix){
		if(content == null || "".equals(content.trim())) return null;
		int start = content.indexOf(variablePrefix);
		int end = content.indexOf(variableSurfix, start+1);
		while(start > -1 && end > -1){
			String name = content.substring(start + 1,end);
			content = content.replace(variablePrefix + name + variableSurfix, getEnviromentVariable(name));
			start = content.indexOf(variablePrefix);
			end = content.indexOf(variableSurfix,start+1);
		}
		return content;
	}
    /**
     * 获取指定的环境变量的值
     * @param name
     * @return
     */
	protected String getEnviromentVariable(String name){
		return EnviromentHolder.getEnviroment().getVariableString(name);
	}
	
}
